package com.zt.common.pojo.common;

import com.alibaba.fastjson.JSONObject;
import com.zt.common.common.BaseErrorInfo;
import com.zt.common.common.CodeMsg;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.stereotype.Component;

/**
 * description:
 * author:张腾
 * date:2021-04-22
 */

@Data
@Accessors(chain = true)
@Component
public class JsonResult<T> {

    //-1 -99 -999
    private String code;

    private String msg;

    private T data;

    private int total;  //可以为空


    public JsonResult(){
    }

    public JsonResult(BaseErrorInfo baseErrorInfo){
        this.code = baseErrorInfo.getResultCode();
        this.msg = baseErrorInfo.getResultMsg();
    }


    /**
     * 成功
     * @return
     */
    public static JsonResult success(){
        return success(null);
    }

    /**
     * 成功 带有data
     * @param data
     * @return
     */
    public static <T> JsonResult<T> success(T data) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(CodeMsg.SUCCESS.getResultCode());
        result.setMsg(CodeMsg.SUCCESS.getResultMsg());
        result.setData(data);

        return result;
    }

    /**
     * 成功 带有data以及数据量参数
     * @param data
     * @param total
     * @return
     */
    public static <T> JsonResult<T> success(T data,int total) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(CodeMsg.SUCCESS.getResultCode());
        result.setMsg(CodeMsg.SUCCESS.getResultMsg());
        result.setData(data);
        result.setTotal(total);

        return result;
    }

    /**
     * 失败
     */
    public static <T> JsonResult<T> error(BaseErrorInfo errorInfo) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(errorInfo.getResultCode());
        result.setMsg(errorInfo.getResultMsg());
        result.setData(null);
        return result;
    }

    public static <T> JsonResult<T> error(BaseErrorInfo errorInfo,T data) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(errorInfo.getResultCode());
        result.setMsg(errorInfo.getResultMsg());
        result.setData(data);
        return result;
    }

    /**
     * 失败
     */
    public static <T> JsonResult<T> error(String code, String message) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(code);
        result.setMsg(message);
        result.setData(null);
        return result;
    }

    /**
     * 失败
     */
    public static <T> JsonResult<T> error(String message) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode("-1");
        result.setMsg(message);
        result.setData(null);
        return result;
    }

    /**
     * 失败
     */
    public static <T> JsonResult<T> error(String code,String message,T data) {
        JsonResult<T> result = new JsonResult<>();
        result.setCode(code);
        result.setMsg(message);
        result.setData(data);
        return result;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
